﻿using Md.Api.DDD;
using Md.Api.Model.Web.MdUser.Request;
using Md.Dapper.Repositories.Md;
using Md.Redis.Repositories;

namespace Md.Api.Service.Web.MdUser;
/// <summary>
/// 忘记用户密码服务
/// </summary>
public class ForgetMdUserPwdWebService : ApiBase<RequestForgetMdUserPwdWebModel>
{
    /// <summary>
    /// 用户仓储
    /// </summary>
    public MdUserRep mdUserRep { get; set; }
    /// <summary>
    /// redis缓存
    /// </summary>
    public MdRedisService _redisService { get; set; }
    /// <summary>
    /// 执行
    /// </summary>
    protected override void ExecuteMethod()
    {
        if (string.IsNullOrWhiteSpace(_redisService.Instance.Get(Parameter.CodeUuid)))
        {
            throw new AggregateException("验证码已超时");
        }

        if (Parameter.Code != _redisService.Instance.Get(Parameter.CodeUuid))
        {
            throw new AggregateException("验证码不正确");
        }

        var userId = mdUserRep.CheckEmailUnique(Parameter.Email);

        if (Convert.ToInt64(userId) <= 0)
        {
            throw new AggregateException("用户不存在");
        }

        var res = mdUserRep.UpdateMdUserPwdByUserId(Convert.ToInt64(userId), EncrptyHelp.MD5Pwd(Parameter.UserPwd));

        Result.Data = res;
    }
}

